package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.kaishengit.entity.Task;
import com.kaishengit.util.DBHelp;
import com.kaishengit.util.RowMapper;

public class TaskDao {
	
	private DBHelp<Task> db = new DBHelp<Task>();
	
	public void Save (String id,String name,String desc,String begintime,String endtime,
			String state,String level,int schedule,String createtime,String t_employee_id,
			String t_goal_id,String t_goal_t_project_id) {
		String sql = "insert into t_task(id,name,desc,begintime,endtime,state,level,schedule,createtime,t_employee_id,t_goal_id,t_goal_t_project_id) values(?,?,?,?,?,?,?,?,?,?,?,?)";
		db.executeSQL(sql, id,name,desc,begintime,endtime,state,level,schedule,createtime,t_employee_id,t_goal_id,t_goal_t_project_id);
	}
	
	public void Del (String id) {
		String sql = "delete from t_task where id = ?";
		db.executeSQL(sql, id);
	}
	
	public void Update (Task task) {
		String sql = "update t_task set name = ?,desc = ?,begintime = ?,endtime = ?,state = ?,level = ?,schedule = ?,createtime = ?,t_employee_id = ?,t_goal_id = ?,t_goal_t_project_id = ? where id = ?";
		db.executeSQL(sql, task.getName(),task.getDesc(),task.getBegintime(),task.getEndtime(),task.getState(),task.getLevel(),task.getSchedule(),task.getCreatetime(),task.getT_employee_id(),task.getT_goal_id(),task.getT_goal_t_project_id(),task.getId());
	}
	
	public Task FindById (String id) {
		String sql = "select name,desc,begintime,endtime,state,level,schedule,createtime,t_employee_id,t_goal_id,t_goal_t_project_id from t_task where id = ?";
		return db.executeQueryForObject(sql, new TaskRowMapper(), id);
	}
	
	private class TaskRowMapper implements RowMapper<Task> {
		
		public Task mapperRow (ResultSet rs) throws SQLException {
			
			Task task = new Task();
			task.setId(rs.getString("id"));
			task.setName(rs.getString("name"));
			task.setDesc(rs.getString("desc"));
			task.setBegintime(rs.getString("begintime"));
			task.setEndtime(rs.getString("endtime"));
			task.setState(rs.getString("state"));
			task.setLevel(rs.getString("level"));
			task.setSchedule(rs.getInt("schedule"));
			task.setCreatetime(rs.getString("createtime"));
			task.setT_employee_id(rs.getString("t_employee_id"));
			task.setT_goal_id(rs.getString("t_goal_id"));
			task.setT_goal_t_project_id(rs.getString("t_goal_t_project_id"));
			return task;
		}
	}
}
